﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using lauimini_webform.Data;
using lauimini_webform.Models;

namespace lauimini_webform.BLL
{
    public class UserTargetService
    {
        MyDBContext context;
        public UserTargetService()
        {
            context = new MyDBContext();
        }
        public int addTarget(string Title, string description, string StartTime, string EndTime, int userid)
        {
            UserTarget target = new UserTarget();
            target.Title = Title;
            target.StartTime = StartTime;
            if (Convert.ToDateTime(target.StartTime).Hour < 7 || Convert.ToDateTime(target.StartTime).Hour >= 23)
            {
                return 0;
            }
            target.EndTime = EndTime;
            target.userid = userid;
            target.description = description;
            target.Status = "未完成";
            target.DayOfWeek = Convert.ToDateTime(target.StartTime).DayOfWeek.ToString();

            //计算预计花费多少时间完成任务：
            DateTime endTime = Convert.ToDateTime(EndTime);
            DateTime startTime = Convert.ToDateTime(StartTime);
            TimeSpan timeSpan = endTime - startTime;
            if (timeSpan.Days >= 0)
            {
                target.CosTime = timeSpan.Hours + timeSpan.Days * 24;
            }
            else
            {
                target.CosTime = timeSpan.Hours;
            }

            if (target.CosTime <= 0)
            {
                //target.CosTime += 24;
                return -1;
            }

            context.userTargets.Add(target);
            context.SaveChanges();
            return 200;
        }
        /// <summary>
        /// 修改待办
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="description"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public int editTarget(int id,string Title,string description,string StartTime,string EndTime)
        {
            UserTarget userTarget = context.userTargets.Find(id);

            userTarget.Title = Title;
            userTarget.description = description;
            userTarget.StartTime = StartTime;
            userTarget.EndTime = EndTime;
            //计算预计花费多少时间完成任务：
            DateTime endTime = Convert.ToDateTime(EndTime);
            DateTime startTime = Convert.ToDateTime(StartTime);
            TimeSpan timeSpan = endTime - startTime;
            if (timeSpan.Days >= 0)
            {
                userTarget.CosTime = timeSpan.Hours + timeSpan.Days * 24;
            }
            else
            {
                userTarget.CosTime = timeSpan.Hours;
            }

            if (userTarget.CosTime <= 0)
            {
                //target.CosTime += 24;
                return -1;
            }
            context.SaveChanges();
            return userTarget.id;
        }
        public bool deleteTarget(int id)
        {
            UserTarget userTarget = context.userTargets.Where(x => x.id == id).FirstOrDefault();
            context.userTargets.Remove(userTarget);
            context.SaveChanges();
            return true;
        }
        /// <summary>
        /// 获取我所有的待办
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public List<UserTarget> getMyTarget(int userid)
        {
            List<UserTarget> userTargets = context.userTargets.Where(x => x.userid == userid).ToList();
            return userTargets;
        }

        /// <summary>
        /// 获取我所有未完成的待办
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public List<UserTarget> getNoFinishMyTarget(int userid)
        {
            List<UserTarget> userTargets = context.userTargets.Where(x => x.userid == userid && x.Status == "未完成").ToList();
            return userTargets;
        }
        /// <summary>
        /// 获取我所有已完成的待办
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public List<UserTarget> getFinishMyTarget(int userid)
        {
            List<UserTarget> userTargets = context.userTargets.Where(x => x.userid == userid && x.Status == "已完成").ToList();
            return userTargets;
        }

        /// <summary>
        /// 获取每月未/完成数
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="parMonth"></param>
        /// <returns></returns>
        public int getMonthFinishMyTargetCount(int userid, int parMonth, string status)
        {
            List<UserTarget> userTargets = context.userTargets.Where(x => x.userid == userid && x.Status == status).ToList();
            List<UserTarget> targets = new List<UserTarget>();
            foreach (var item in userTargets)
            {
                int month = Convert.ToDateTime(item.RealEndTime).Month;
                if (parMonth == month)
                {
                    targets.Add(item);
                }
            }
            return targets.Count;
        }

        /// <summary>
        /// 获取每星期未/完成数
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="parMonth"></param>
        /// <returns></returns>
        public int getWeekFinishMyTargetCount(int userid, string Week, string status)
        {
            List<UserTarget> userTargets = context.userTargets.Where(x => x.userid == userid && x.Status == status).ToList();
            List<UserTarget> targets = new List<UserTarget>();
            foreach (var item in userTargets)
            {

                if (Week == item.DayOfWeek)
                {
                    targets.Add(item);
                }
            }
            return targets.Count;
        }





        /// <summary>
        /// 完成我的待办
        /// </summary>
        /// <param name="targetId"></param>
        /// <returns> false用户还没点击开始任务 代表</returns>
        public bool finishMyTarget(int targetId)
        {
            UserTarget userTarget = context.userTargets.FirstOrDefault(x => x.id == targetId);
            userTarget.Status = "已完成";
            if (string.IsNullOrEmpty(userTarget.RealStartTime))
            {
                return false;
            }
            userTarget.RealEndTime = DateTime.Now.ToString();
            DateTime RealEnd = Convert.ToDateTime(userTarget.RealEndTime);
            DateTime RealStart = Convert.ToDateTime(userTarget.StartTime);
            TimeSpan timeSpan = RealEnd - RealStart;
            userTarget.RealCosTime = timeSpan.Hours;
            context.SaveChanges();
            return true;
        }

        /// <summary>
        /// 开始我的待办
        /// </summary>
        /// <param name="targetId"></param>
        /// <returns>false 说明已经有过开始了</returns>
        public bool startMyTarget(int targetId)
        {
            UserTarget userTarget = context.userTargets.FirstOrDefault(x => x.id == targetId);
            if (userTarget.RealStartTime != null)
            {
                return false;
            }
            userTarget.RealStartTime = DateTime.Now.ToString();
            context.SaveChanges();
            return true;
        }
        /// <summary>
        /// 获取日程表对应格子的内容
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="week"></param>
        /// <param name="timeStart"></param>
        /// <param name="timeEnd"></param>
        /// <returns></returns>
        public List<UserTarget> getPlanList(int userid, string week, double timeStart, double timeEnd)
        {
            List<UserTarget> userTargets = context.userTargets.Where(x => x.userid == userid && x.DayOfWeek == week && x.Status == "未完成").ToList();
            List<UserTarget> planList = new List<UserTarget>();
            foreach (var item in userTargets)
            {
                int hourStart = Convert.ToDateTime(item.StartTime).Hour;
                int hourEnd = Convert.ToDateTime(item.EndTime).Hour;
                if (hourStart >= timeStart && hourStart <= timeEnd)
                {
                    planList.Add(item);
                }
            }
            return planList;
        }

        /// <summary>
        /// 获取每星期完成任务的的个数
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="week"></param>
        /// <returns></returns>
        public int getWeekFinishCount(int userid, string week)
        {
            var target = context.userTargets.Where(x => x.userid == userid && x.DayOfWeek == week && x.Status == "已完成").ToList();
            int count = target.Count;
            return count;
        }

    }
}